package utils
{
import flash.utils.Dictionary;
import flash.utils.describeType;
import flash.utils.getQualifiedClassName;

import values.avatar.AvatarPropertyPluserType;
import values.avatar.EnhancePropertyPluser;
import values.avatar.IAvatarPropertyPluser;
import values.avatar.IAvatarValue;
import values.avatar.IPropertyProvider;

/**
 * EquipUtil
 * @author jliang
 *
 */
public class PropertyUtil
{
	private static var defMap:Dictionary = new Dictionary(true);   /* Object */
	
	/**
	 * 处理角色额外属性
	 * @param avatar
	 * @param pluser
	 * @param propertyProvider
	 * @param flag              1 -> add;   -1 -> remove
	 *  
	 */	
	public static function updateAvatarPluserProperty(avatar:IAvatarValue, pluser:IAvatarPropertyPluser, propertyProvider:IPropertyProvider,  flag:int = 1):void
	{
		var metadataList:Dictionary = initPropertyMapping(propertyProvider);
		
		var properties:Array = [];
		
		for each (var mdObj:Object in metadataList)
		{
			if (propertyProvider[mdObj.prop] == 0) continue;
			
			pluser[mdObj.mapping] = pluser[mdObj.mapping] + flag * propertyProvider[mdObj.prop];
			
			properties.push(mdObj.mapping);
		}
		
		avatar.refreshProperties(properties.length == 0 ? null : properties);
	}
	
	/**
	 * 处理额外属性加成 
	 * @param pluser
	 * @param properties    Array / Vector [String]
	 * @return 
	 * 
	 */	
	public static function handlePluserProperty(pluser:IAvatarPropertyPluser, properties:* = null):Object
	{
		var metadataList:Dictionary = initPropertyMapping(pluser);
		
		var obj:Object = {};
		
		var mdObj:Object;
		
		var temp:Number;
		
		if (properties != null)
		{			
			for each (var pstr:String in properties)
			{				
				mdObj = metadataList[pstr];
				
				if (mdObj == null)
				{
					continue;
				}
				
				temp = pluser[mdObj.prop];
				
				obj[mdObj.prop] = { mapping:mdObj.mapping, value:temp };
			}
		}
		else
		{
			for each (mdObj in metadataList)
			{				
				temp = pluser[mdObj.prop];
				
				obj[mdObj.prop] = { mapping:mdObj.mapping, value:temp };
			}
		}
		
		
		return obj;
	}
	
	/**
	 *  
	 * @param propertyProvider
	 * @return Dictionary  key : mapping  { mapping: mapping, prop:propName, field:xml def (可选) }
	 * 
	 */	
	public static function initPropertyMapping(propertyProvider:Object):Dictionary
	{
		var ref:String = getQualifiedClassName(propertyProvider);
		
		var metadataList:Dictionary = defMap[ref];
		
		if (!metadataList)
		{
			metadataList = new Dictionary();
			
			var def:XML = describeType(propertyProvider);
			var accessors:XMLList = def.accessor;
			
			for each (var acc:* in accessors)
			{
				var propName:String = String(acc.@name);
				var metadata:* = acc.metadata.(@name == "Prop");
				if (metadata.toXMLString() != "")
				{
					var mapping:String = String(metadata.arg.(@key == "mapping").@value);
					var field:String = String(metadata.arg.(@key == "field").@value);
					var obj:Object = { mapping: mapping, prop:propName };
					
					if (field != null && field != "")
					{
						obj.field = field;
					}
					
					metadataList[mapping] = obj;
				}
			}
			
			defMap[ref] = metadataList;
		}
		
		return metadataList;
	}
	
	/**
	 *  获取指定角色的强化属性加成
	 * @param avatar
	 * @return [ 战斗, 特技, 元素 ]
	 * 
	 */	
	public static function getAvatarEnhanceProperties(avatar:IAvatarValue):Array
	{
		return EnhancePropertyPluser(avatar.getPropertyPluserByKey(AvatarPropertyPluserType.ENHANCE)).enhanceProperties;
	}
}
}



